﻿#include "UDeviceMainPage.h"
#include <QScrollArea>
#include <QFileDialog>
#include "UToolBarManager.h"
#include "Global.h"
#include "UBaseNode.h"
#include "UPointInfo.h"
#include "MyToolButton.h"
#include "UPropertyNode.h"
UDeviceMainPage::UDeviceMainPage(QWidget *parent)
	: QWidget(parent)
{
	ui.setupUi(this);
	m_pNodeManager = ui.widget;
	m_pCentWidget = NULL;
	m_pParrentNode = nullptr;
	m_pCurrNode = nullptr;
}

UDeviceMainPage::~UDeviceMainPage()
{}

void UDeviceMainPage::initUi()
{
	m_pPointTable = new UPointTable(this);
	m_pPointTable->installEventFilter(this);
	ui.scrollArea_2->setWidget(m_pPointTable);
	// 设置内容边距（上下左右）
	ui.scrollArea_2->widget()->setContentsMargins(0, 0, 0, 0);
	connect(ui.widget, &UNodeManager::sign_showCilckedWidget, this, &UDeviceMainPage::slot_showCilckedWidget);
	ui.widget->initUI();
	ui.toolButton_3->setIcon(QIcon(":/SmartControl/Resource/toolbar/run.png"));
	ui.toolButton_4->setIcon(QIcon(":/SmartControl/Resource/toolbar/stop.png"));
	ui.toolButton_3->setToolTip(A2T("开始监视")); // 鼠标悬停显示提示	
	ui.toolButton_4->setToolTip(A2T("停止监视")); // 鼠标悬停显示提示
	ui.splitter_2->widget(0)->setMinimumWidth(20); // 左边最小 200
	ui.splitter_2->widget(1)->setMinimumWidth(20); // 右边最小 300
	ui.splitter_2->setCollapsible(0, false);
	ui.splitter_2->setCollapsible(1, false);

	ui.splitter->widget(0)->setMinimumHeight(20); // 左边最小 200
	ui.splitter->widget(1)->setMinimumHeight(20); // 右边最小 300
	ui.splitter->setCollapsible(0, false);
	ui.splitter->setCollapsible(1, false);
}

void UDeviceMainPage::destroyUi()
{
	m_pPointTable->stopRefush();
}

void UDeviceMainPage::refushUi()
{
	m_pConfig = nullptr;
	m_pCurrNode = nullptr;
	m_pParrentNode = nullptr;
	m_pCentWidget = nullptr;
	m_pPointTable->startRefush();
	m_pNodeManager->refushUi();
}

void UDeviceMainPage::showPointTable()
{
	QList<CMeasurePoint*> pVecMeasurePoint;
	if (m_pCurrNode == nullptr)
	{
		m_pPointTable->setTablePoint(pVecMeasurePoint);
		return;
	}	
	if (m_pCurrNode->getType() == CBaseNode::enRoot || m_pCurrNode->getType() == CBaseNode::enNode)
	{
		QVector<CBaseNode *> pVecNode = m_pCurrNode->getChildren();
		for (int i = 0; i < pVecNode.size(); i++)
		{
			QWidget* pIconWidget = nullptr;

			if (pVecNode[i]->getType() == CBaseNode::enMeasurePoint)
			{
				pVecMeasurePoint.push_back((CMeasurePoint*)pVecNode[i]);
				continue;
			}
		}
		m_pPointTable->setTablePoint(pVecMeasurePoint);
	}
	else if (m_pCurrNode->getType() == CBaseNode::enDevice)
	{
		if (((CBaseDevice *)m_pCurrNode)->getDevice() == nullptr)
		{
			return;
		}
		QList<CMeasurePoint*> pVecMeasurePoint = ((CBaseDevice *)m_pCurrNode)->getDevice()->getMeasurePoints();
		m_pPointTable->setTablePoint(pVecMeasurePoint);
	}
}

/**
* @ Author: Gaojia
* @ Description: 鼠标单击响应函数
*/
void UDeviceMainPage::slot_clicked(CBaseNode * pNode)
{
	if (pNode->getParent() == nullptr)
	{
		return;
	}
	ui.widget->setCurrNode(pNode->getParent(),pNode);
	m_pCurrNode = pNode;
	showPointTable();
}

/**
* @ Author: Gaojia
* @ Description: 鼠标双击响应函数
*/
void UDeviceMainPage::slot_doubleClicked(CBaseNode * pNode)
{
	ui.widget->setCurrNode(pNode);
	slot_showCilckedWidget(pNode);
}

void UDeviceMainPage::onCentralWidgetRightClick(const QPoint& pos)
{
	g_pToolBarMang->setToolBtn(UToolBarManager::Device, 1);
	if (m_pParrentNode == nullptr)
	{
		return;
	}
	ui.widget->setCurrNode(m_pParrentNode);

	QWidget* child = m_pCentWidget->childAt(pos);

	// 如果点击在 MyToolButton 上，就不弹菜单
	if (qobject_cast<MyToolButton*>(child)) {
		return;
	}
	// 点击在空白处，弹菜单
	QMenu* menu = new QMenu(this);

	// 通用菜单
	menu->addAction(QIcon(":/SmartControl/Resource/toolbar/add.png"), A2T("添加节点(N)..."), this, [=]()
		{
			if (m_pNodeManager != nullptr)
			{
				m_pNodeManager->addNode(m_pParrentNode);
			}
		});
	menu->addAction(QIcon(":/SmartControl/Resource/toolbar/adddevice.png"), A2T("添加设备(D)..."),  this, [=]()
		{
			if (m_pNodeManager != nullptr)
			{
				m_pNodeManager->addDevice(m_pParrentNode);
			}
		});
	menu->addSeparator();
	menu->addAction(A2T("刷新"), this, [=]()
		{
			if (m_pNodeManager != nullptr)
			{
				slot_showCilckedWidget(m_pParrentNode);
			}
		});
	menu->addSeparator();
	menu->addAction(A2T("属性"), this, [=]()
		{
			UPropertyNode dlg(m_pCurrNode, this);  // this 是父窗口
			dlg.exec();
		});
	menu->exec(m_pCentWidget->mapToGlobal(pos));
	delete menu;
}

void UDeviceMainPage::slot_deleteNode(CBaseNode* pNode)
{
	m_pNodeManager->deleteNode(pNode);
}



void UDeviceMainPage::slot_showCilckedWidget(CBaseNode *pNode, CBaseNode* pchild)
{
	CBaseNode *pNameNode = pNode;
	QString sName = "";
	while (pNameNode != nullptr)
	{
		sName = pNameNode->getName() + " > " + sName;
		pNameNode = pNameNode->getParent();
	}
	ui.label->setText(sName);
	QWidget* container = new QWidget();
	m_pCentWidget = container;
	container->installEventFilter(this);
	container->setContextMenuPolicy(Qt::CustomContextMenu);
	connect(container, &QWidget::customContextMenuRequested, this, &UDeviceMainPage::onCentralWidgetRightClick);
	QGridLayout* layout = new QGridLayout(container);
	layout->setAlignment(Qt::AlignTop | Qt::AlignLeft);
	layout->setHorizontalSpacing(15); // 控件间距
	layout->setVerticalSpacing(15);   // 左边距 50px，其他方向 0
	layout->setContentsMargins(15, 15, 0, 15);
	m_pParrentNode = pNode;
	if (pNode->getType() == CBaseNode::enRoot || pNode->getType() == CBaseNode::enNode)
	{
		m_mapToolBtn.clear();
		QVector<CBaseNode *> pVecNode = pNode->getChildren();
		const int maxRows = 3;
		const int maxCols = 10;
		int row = 0, col = 0;
		int itemCount = 0;
		QButtonGroup *btnGroup = new QButtonGroup(this);
		btnGroup->setExclusive(true);
		for (int i = 0; i < pVecNode.size() && row < maxRows; i++)
		{
			QWidget* pIconWidget = nullptr;
			if (pVecNode[i]->getType() == CBaseNode::enNode || pVecNode[i]->getType() == CBaseNode::enRoot)
			{
				pIconWidget = new MyToolButton((CBaseNode *)pVecNode[i], btnGroup);
				connect(pIconWidget, SIGNAL(sign_clicked(CBaseNode *)), this, SLOT(slot_clicked(CBaseNode *)));
				connect(pIconWidget, SIGNAL(sign_doubleClicked(CBaseNode *)), this, SLOT(slot_doubleClicked(CBaseNode *)));	
				connect(pIconWidget, SIGNAL(sign_Delete(CBaseNode*)), this, SLOT(slot_deleteNode(CBaseNode*)));
				btnGroup->addButton((MyToolButton *)pIconWidget);
			}
			else if (pVecNode[i]->getType() == CBaseNode::enDevice)
			{
				pIconWidget = new MyToolButton((CBaseNode *)pVecNode[i], btnGroup);
				connect(pIconWidget, SIGNAL(sign_clicked(CBaseNode *)), this, SLOT(slot_clicked(CBaseNode *)));
				connect(pIconWidget, SIGNAL(sign_Delete(CBaseNode*)), this, SLOT(slot_deleteNode(CBaseNode*)));
				btnGroup->addButton((MyToolButton *)pIconWidget);
			}

			if (pIconWidget)
			{
				pIconWidget->setMinimumSize(120, 120);
				pIconWidget->setMaximumSize(120, 120);
				layout->addWidget(pIconWidget, row, col);
				col++;
				itemCount++;

				if (col >= maxCols)
				{
					col = 0;
					row++;
				}
			}
			if (pIconWidget != nullptr)
			{
				m_mapToolBtn.insert(pVecNode[i]->getName(), (MyToolButton*)pIconWidget);
			}
		}

		// 补空白控件填满剩余网格
		for (; row < maxRows; row++)
		{
			for (; col < maxCols; col++)
			{
				QWidget* blank = new QWidget();
				blank->setMinimumSize(120, 120);
				blank->setMaximumSize(120, 120);
				layout->addWidget(blank, row, col);
			}
			col = 0;
		}
		container->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum);
		ui.scrollArea->setWidgetResizable(true);
		ui.scrollArea->setWidget(container);
	}
	if (pchild != nullptr)
	{
		if (m_mapToolBtn.contains(pchild->getName()))
		{
			auto btn = static_cast<MyToolButton*>(m_mapToolBtn[pchild->getName()]);
			btn->animateClick(100);
		}
		slot_clicked(pchild);
	}
	else
	{
		m_pCurrNode = NULL;
		showPointTable();
	}
}


bool UDeviceMainPage::eventFilter(QObject* obj, QEvent* event)
{
	if (event->type() == QEvent::MouseButtonRelease) 
	{
		if (obj == m_pCentWidget)
		{
			QMouseEvent* mouseEvent = static_cast<QMouseEvent*>(event);
			if (mouseEvent->button() == Qt::LeftButton)
			{
				g_pToolBarMang->setToolBtn(UToolBarManager::Device, 1);
			}
		}
	}
	return QWidget::eventFilter(obj, event);
}